package gs.wicket.app.product;

import java.util.Arrays;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

public class Konvertor extends ProductsPage{

	List<String> valute = Arrays.asList("KM","USD","GBP");


	public Konvertor() {

		
		final RequiredTextField euro = new RequiredTextField("euro", new Model());
		final TextField rezultat = new TextField("rezultat", new Model());
		final DropDownChoice valuta = new DropDownChoice("valuta", new Model(),valute);
		
		Form form = new Form("form");
		add(form);
		add(new Label("konvertor.title", getLangProperty("konvertor.title")));
		add(new Label("konvertor.message", getLangProperty("konvertor.message")));
		
		form.add(euro);
		form.add(valuta);
		form.add(rezultat);
		form.add(new Label("konvertor.currency", getLangProperty("konvertor.currency")));
		
		form.add(new Link("cancel") {
			
			private static final long serialVersionUID = 1L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("konvertor.cancel"))));
			}
			@Override
			public void onClick() {
				setResponsePage(Konvertor.class);
			}
		});
		form.add(new Button("convert") {
			
			private static final long serialVersionUID = 1L;
			{
				add(new AttributeModifier("value", new Model(getLangProperty("konvertor.convert"))));
			}
			@Override
			public void onSubmit() {
				String temp;
				String val = valuta.getModelObjectAsString();
				Double rez1 = Double.parseDouble(euro.getInput());
				if(rez1 < 0){
					info(val);
				}
				else if(val.matches("KM")){
					temp = Double.toString(rez1*1.95583);
					rezultat.setModelValue(temp);
				}
				else if(val.matches("USD")){
					temp = Double.toString(rez1*1.2694);
					rezultat.setModelValue(temp);
				}
				else if(val.matches("GBP")){
					temp = Double.toString(rez1*0.8821);
					rezultat.setModelValue(temp);
				}
				
				}
		});
	}
	
}
